package mh.modules.mh.service.impl;

import mh.common.utils.R;
import mh.modules.mh.dao.BcDao;
import mh.modules.mh.entity.BcEntity;
import mh.modules.mh.service.BcService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mh.common.utils.PageUtils;
import mh.common.utils.Query;

import mh.modules.mh.dao.GrowthDao;
import mh.modules.mh.entity.GrowthEntity;
import mh.modules.mh.service.GrowthService;


@Service("growthService")
public class GrowthServiceImpl extends ServiceImpl<GrowthDao, GrowthEntity> implements GrowthService {
    @Autowired
    private GrowthService growthService;
    @Autowired
    private  GrowthDao growthDao;
    @Autowired
    private  BcDao bcDao;
    @Autowired
    private BcService bcService;

    /**
     * 通过编号查信息
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GrowthEntity> page = this.page(
                new Query<GrowthEntity>().getPage(params),
                new QueryWrapper<GrowthEntity>().like(StringUtils.isNotBlank((String) params.get("key")),"mh_bh",params.get("key"))
        );

        return new PageUtils(page);
    }

    /**
     *
     * @param growth TODO 每次添加数据，与株树相关的数据都要重新计算并更新（完成后修改这个BUG）
     * @return
     */
    @Override
    public boolean saveGrowth(GrowthEntity growth) {
        long l = System.currentTimeMillis();
        String mhBh = "mh"+l;
        growth.setMhBh(mhBh);
        // 总数
        Long mhNumber = growthDao.getTotal();
        mhNumber = mhNumber+1;
        growth.setMhNumber(mhNumber);
        //更新所有数据的总数
        boolean update1 = growthDao.updateAllMhGrowth(mhNumber);
        boolean update2 = bcDao.updateAllMhBc(mhNumber);
        //更新总数量变化更新所有通过总数量计算的数据 TODO
        //质量
        List<GrowthEntity> list = growthService.list(null);
        for(GrowthEntity growth1 : list){
            Long mhNumber1 = growth1.getMhNumber();  //总株树
            Long mhZls = growth1.getMhZls();  // 总铃数
            growth1.setMhDzjls(mhZls/mhNumber1);
            growthService.updateById(growth1);
        }
        //病虫 平均螨害级数（与总株树无关）
        List<BcEntity> list1 = bcService.list(null);
        for (BcEntity bc : list1){
            Long mhNumber1 = bc.getMhNumber();  //总株树
            Long bcMhzs = bc.getBcMhzs();  //螨害株数
            bc.setBcTjmhzl((((double)bcMhzs/mhNumber1))*100);
            bc.setBcYmzl((((double)bcMhzs/mhNumber1))*100);
            bcService.updateById(bc);
        }
        //单株结铃数 =  总铃数除以株数（取整）个
        Long mhZls = growth.getMhZls(); // 总铃数
        long mhDzjls = mhZls / mhNumber;
        growth.setMhDzjls(mhDzjls);
        boolean save = growthService.save(growth);
        return save;
    }

    /**
     * 生长和质量一个方法
     * 1.如果生长的信息的 总铃数 或者 株数 改变需要在这里也是实现
     * 2.质量的（衣分 =（皮棉重量 ÷  籽棉重量）×100%。）需要不断更新
     * @param growth
     * @return
     */
    @Override
    public boolean updateGrowth(GrowthEntity growth) {
        // 总数
        Long mhNumber = growthDao.getTotal();
        //单株结铃数 =  总铃数除以株数（取整）个
        Long mhZls = growth.getMhZls(); // 总铃数
        long mhDzjls = mhZls / mhNumber;
        growth.setMhDzjls(mhDzjls);
        boolean save = growthService.updateById(growth);
        return save;
    }

    /**
     * //1.删除后更新总株树
     * 2.更新每条数据的单株结铃数
     * @param asList
     * @return
     */
    @Override
    public boolean removeByGrowthIds(List<Long> asList) {
        int size = asList.size();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        Long id=asList.get(0);
        queryWrapper.eq("mh_id", id);
        GrowthEntity one = growthService.getOne(queryWrapper);
        Long mhNumber = one.getMhNumber();
        mhNumber = mhNumber - size;
        growthDao.updateAllMhGrowth(mhNumber);
        bcDao.updateAllMhBc(mhNumber);
        boolean b = growthService.removeByIds(asList);
        List<GrowthEntity> growthEntities = growthDao.selectList(null);
        //更新单株结玲数
        for(GrowthEntity growth :growthEntities){
            Long mhNumber1 = growth.getMhNumber();
            Long mhZls = growth.getMhZls();
            growth.setMhDzjls(mhZls/mhNumber1);
            growthService.updateById(growth);
        }
        return b;
    }

    /**
     * 柱状图棉花的生长和质量情况
     * @return
     */
    @Override
    public R growthBar() {
        List<GrowthEntity> growthEntities= growthDao.growthBar();
        return R.ok().put("data",growthEntities);
    }


}